#ifndef DFAGRAPH
#define DFAGRAPH

#include <vector>
#include <map>
#include <iostream>
#include "State.h"

using namespace std;
//dfa图
class DFAGraph{
    int node_index=0; //表示当前有几个节点
public:
    std::map<int, StateNode> nodes;
    map<set<int>,int> mp_NFA_DFA; //NFA状态对应的DFA状态的映射

    int insert_state(set<int> &st , StateType stype=StateType::UNMATCH){
        //插入一个状态，当这个状态已存在的时候，返回状态的序号
        if(judge_state_exists(st)){
            return mp_NFA_DFA[st];
        }else{
            nodes[node_index]=move(StateNode(node_index,stype));
            mp_NFA_DFA[st]=node_index;
            node_index++;
            return node_index-1;
        }
    }

    int get_state_index(set<int>&st){
        if(judge_state_exists(st)){
            return mp_NFA_DFA[st];
        }else{
            return -1;
        }
    }

    bool judge_state_exists(set<int> &st){
        //判断一个状态是否存在
        return mp_NFA_DFA.count(st);
    }

    void insert_converstion(set<int> from_st , char convert, set<int> to_st ){
        int start_index=insert_state(from_st);
        int end_index=insert_state(to_st);
        nodes[start_index].transitions.push_back(Transition{convert,end_index});
    }

    
    
    void display_DFA(){
        for(const auto &elem : mp_NFA_DFA){
            if(nodes[elem.second].type==StateType::MATCH){
                cout<<"Final State:\n";
            }else if(nodes[elem.second].type==StateType::START){
                cout<<"Start State:\n";
            }

            cout<<"NFA state {"<<show_state(elem.first)<<"}  DFA state index {"<<elem.second<<"}\n";
            cout<<"conversion :\n";
            StateNode &sn=nodes[elem.second];
            for(Transition &tra : sn.transitions){
                tra.get_info();
            }

            cout<<'\n';
        }
        cout<<"--------------------------------\n";
    }


};

#endif